// pages/home/home.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 当前高亮导航索引
    currentActiveNavIndex: 0,
    // 上一个激活的导航索引
    prevActiveNavIndex: -1,
    // scroll-view 横向滚动条位置
    scrollLeft: 0,
    // 是否显示骨架屏
    loading: true,
    // 轮播图指示器高亮下标
    current:0,

    // 头部导航栏的高度
    statusBarHeight: app.globalData.statusBarHeight,
    

    // 轮播图
    bannerData:[
      {
        Img:"//cdn.cnbj1.fds.api.mi-img.com/mi-mall/84a465bbe4ef6a5bf2f429eaf289a8d1.jpg?f=webp&w=1080&h=540&bg=F3EFE4"
      },
      {
        Img:"//cdn.cnbj1.fds.api.mi-img.com/mi-mall/447e5958b7d65621921597843aceca78.jpg?f=webp&w=1080&h=540&bg=FFFFFF"
      },
      {
        Img:"//cdn.cnbj1.fds.api.mi-img.com/mi-mall/1fdf5f4c327d71ce20dc6f8cc5250d94.jpg?f=webp&w=1080&h=540&bg=DAEAEA"
      },
      {
        Img:"//cdn.cnbj1.fds.api.mi-img.com/mi-mall/9cbe454df0f4b3a29530d311dfc75c18.jpg?f=webp&w=1080&h=540&bg=6FA2C1"
      },
      {
        Img:"//cdn.cnbj1.fds.api.mi-img.com/mi-mall/65fe58ff056ed8ce643f70d1367b6d6c.jpg?f=webp&w=1080&h=540&bg=A1117"
      },
      {
        Img:"//cdn.cnbj1.fds.api.mi-img.com/mi-mall/ec79106bd79389bf10d7c7541f913759.jpg?f=webp&w=1080&h=540&bg=50912"
      }
    ],
    // 金刚区
    kingkongData:[
      {
        title:"秋星商城",
        Img:"../../images/01_03.gif"
      },
      {
        title:"手机",
        Img:"../../images/01_05.gif"
      },
      {
        title:"秋星发布",
        Img:"../../images/01_07.gif"
      },
      {
        title:"电视",
        Img:"../../images/01_09.gif"
      },
      {
        title:"空冰洗",
        Img:"../../images/01_11.gif"
      },
      {
        title:"米金商城",
        Img:"../../images/01_18.gif"
      },
      {
        title:"秋星众筹",
        Img:"../../images/01_19.gif"
      },
      {
        title:"笔记本",
        Img:"../../images/01_20.gif"
      },
      {
        title:"生活电器",
        Img:"../../images/01_21.gif"
      },
      {
        title:"智能家居",
        Img:"../../images/01_22.png"
      }
    ],
    // 主页tab商品数据
    homeTabData:[
      {
        title:"精选",
        desc:"商品推荐"
      },
      {
        title:"手机",
        desc:"探索黑科技"
      },
      {
        title:"家电",
        desc:"数码电器"
      }
    ],

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    // 控制骨架屏显示时间
    setTimeout(()=>{
      this.setData({
        loading: false
      })
    },1000)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

    // 轮播图
  // 自定义方法
  setSwiperIndex(event){
    // console.log(event);
    this.setData({
      current:event.detail.current
    })
  },


  // 顶部导航改变事件
  topNavChange(e){
    let nextActiveIndex = e.currentTarget.dataset.currentIndex,
    currentIndex = this.data.currentActiveNavIndex;
    if (currentIndex != nextActiveIndex) {
      this.setData({
        currentActiveNavIndex: nextActiveIndex,
        prevActiveNavIndex: currentIndex
      });
    }
  },
  //   * swiper滑动时触发
  //  * 1、prevIndex != currentIndex 表示的是用手滑动 swiper组件
  //  * 2、prevIndex = currentIndex  表示的是通过点击顶部的导航触发的
  swiperChange(e){
    let prevIndex  = this.data.currentActiveNavIndex,
    currentIndex = e.detail.current;
    this.setData({
      currentActiveNavIndex: currentIndex
    });
    if (prevIndex != currentIndex) {
      this.setData({
        prevActiveNavIndex: prevIndex
      });
    }
    this.scrollTopNav();
  },
  // 滚动顶部的导航栏
  // 1、这个地方是大致估算的
  scrollTopNav(){
    // 当激活的当航小于4个时，不滚动
    if (this.data.currentActiveNavIndex <= 3 && this.data.scrollLeft >= 0) {
      this.setData({
        scrollLeft: 0
      });
    } else {
      /**
       * 当超过4个时，需要判断是向左还是向右滚动，然后做相应的处理
       */
      var plus = this.data.currentActiveNavIndex > this.data.prevActiveNavIndex ? 60 : -60;
      this.setData({
        scrollLeft: this.data.scrollLeft + plus
      });
    }
    // console.info(this.data.currentActiveNavIndex, this.data.prevActiveNavIndex, this.data.scrollLeft);
  },

  goSort(e){
    wx.switchTab({
      url: '../sort/sort',
    })
    console.log(e);
  },

  // 跳搜索
  goSearch(){
    wx.navigateTo({
      url: '../search/search',
    })
  }
  
})